﻿@model List<Role>
@section Header{

}
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>登录名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="username" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>将会成为您唯一的登入名
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>角色</label>
            <div class="layui-input-block">
                @foreach (var item in Model)
                {
                    <input type="checkbox" name="RoleId" value="@item.Id" lay-skin="primary" title="@item.Name">
                }

            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_pass" class="layui-form-label">
                <span class="x-red">*</span>密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="L_pass" name="pass" required="" lay-verify="pass"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                6到16个字符
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
                <span class="x-red">*</span>确认密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="L_repass" name="repass" required="" lay-verify="repass"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <input type="button" class="layui-btn" value="增加" id="btnSave" />
        </div>
    </form>
</div>


@section footer
{
    <script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        $("#btnSave").click(function () {
            if ($("input[name='username']").val() == "") {
                layer.msg("用户名不能为空", { icon: 2});
                return;
            }
            if ($("input[name='username']").val().length > 20) {
                layer.msg("用户名最大长度20", { icon: 2 });
                return;
            }
            if ($(".layui-form-checkbox.layui-form-checked").length<=0) {
                layer.msg("请选择角色", { icon: 2 });
                return;
            }
            if ($("input[name='pass']").val() == "") {
                layer.msg("密码不能为空", { icon: 2 });
                return;
            }
            if ($("input[name='pass']").val().length > 20) {
                layer.msg("密码最大长度20", { icon: 2 });
                return;
            }
            if ($("input[name='repass']").val() == "") {
                layer.msg("密码不能为空", { icon: 2 });
                return;
            }
            if ($("input[name='repass']").val().length > 20) {
                layer.msg("密码最大长度20", { icon: 2 });
                return;
            }
            if ($("input[name='repass']").val() != $("input[name='pass']").val()) {
                layer.msg("两次密码不一致", { icon: 2 });
                return;
            }
            var RoleId = [];
            $(".layui-form-checkbox.layui-form-checked").each(function (index, item) {
                RoleId.push($(item).prev("input[name='RoleId']").val());
                //alert($(item).prev("input[name='RoleId']").val());
            })
           /* */$.post("../adminList/adminadd",
            {
                "username": $("input[name='username']").val(),
                "pass": $("input[name='pass']").val(),
                "repass": $("input[name='repass']").val(),
                "RoleId": RoleId
            }, function (data) {

                if (!data.succeed) {

                    layer.msg(data.msg);

                }
                else
                {
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭当前frame
                    parent.layer.close(index);
                }
             })
        });
    });
    </script>
}
